package org.practice.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.practice.dao.UserDao;
import org.practice.model.User;
import org.practice.util.Constant;
import org.practice.util.CookieUtil;
import org.practice.util.ShopDaoFactory;

@SuppressWarnings("serial")
public class LoginInfoAction extends BaseAction{
	public String execute()throws Exception {
		boolean isLogin=false;
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Constant.SESSION_KEY_USER);
		
		if (user != null) {
			isLogin = true;
		} else {
			String uid = CookieUtil.findCookie(request, Constant.COOKIE_KEY_USERID);
			if (uid != null) {
				UserDao userDao = ShopDaoFactory.createUserDao();
				user = userDao.findUserById(Integer.parseInt(uid));
				if (user != null) {
					session.setAttribute(Constant.SESSION_KEY_USER, user);
					isLogin = true;
				}
			}
		}
		request.setAttribute("isLogin", isLogin);
		return SUCCESS;
	}
	
}
